<template>
  <div class="change-details-container">
    <!-- 页面标题 -->
    <div class="page-header">
      <h3>商品变更详情</h3>
      <el-button type="primary" size="small" @click="$emit('back-to-drugs')">
        <i class="el-icon-back"></i> 返回商品列表
      </el-button>
    </div>

    <!-- 商品基本信息 -->
    <el-card class="drug-info-card" shadow="never">
      <div slot="header" class="clearfix">
        <span>商品基本信息</span>
      </div>
      <el-row :gutter="20">
        <el-col :span="8">
          <div class="info-item">
            <label>货位ID：</label>
            <span>{{ huoweiId }}</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="info-item">
            <label>药品ID：</label>
            <span>{{ drugId }}</span>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="info-item">
            <label>批号：</label>
            <span>{{ drugBatchNo }}</span>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <!-- 查询表单 -->
    <el-form :model="queryForm" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="变更类型" prop="changeType">
        <el-select v-model="queryForm.changeType" placeholder="请选择变更类型" clearable>
          <el-option label="正常" value="正常" />
          <el-option label="数量更改" value="数量更改" />
          <el-option label="货位调换" value="货位调换" />
          <el-option label="效期更改" value="效期更改" />
          <el-option label="批号更改" value="批号更改" />
          <el-option label="其它信息" value="其它信息" />
        </el-select>
      </el-form-item>
      <el-form-item label="检查时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 变更详情表格 -->
    <el-table v-loading="loading" :data="changeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="药品名称" align="center" prop="drugName" min-width="150" />
      <el-table-column label="变更类型" align="center" prop="changeType">
        <template slot-scope="scope">
          <el-tag :type="getChangeTypeTagType(scope.row.changeType)">
            {{ scope.row.changeType }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="旧值" align="center" prop="oldValue" min-width="120" />
      <el-table-column label="新值" align="center" prop="newValue" min-width="120" />
      <el-table-column label="数量" align="center" prop="drugNum" />
      <el-table-column label="生产日期" align="center" prop="drugPd" />
      <el-table-column label="有效期" align="center" prop="drugExp" />
      <el-table-column label="药品状态" align="center" prop="drugState" />
      <el-table-column label="操作人" align="center" prop="userName" />
      <el-table-column label="检查时间" align="center" prop="checkTime" width="160">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.checkTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注1" align="center" prop="drugRemark1" min-width="120" />
      <el-table-column label="备注2" align="center" prop="drugRemark2" min-width="120" />
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryForm.pageNum"
      :limit.sync="queryForm.pageSize"
      :page-sizes="[50, 100, 200, 500]"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getChangeDetails } from '@/api/report/inventoryChange'
import { parseTime } from '@/utils/ruoyi'
import Pagination from '@/components/Pagination'
import RightToolbar from '@/components/RightToolbar'

export default {
  name: 'ChangeDetails',
  components: {
    Pagination,
    RightToolbar
  },

  props: {
    huoweiId: {
      type: String,
      required: true
    },
    drugId: {
      type: String,
      required: true
    },
    drugBatchNo: {
      type: String,
      required: true
    },
    queryParams: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 变更详情表格数据
      changeList: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryForm: {
        pageNum: 1,
        pageSize: 50,
        huoweiId: '',
        drugId: '',
        drugBatchNo: '',
        changeType: '',
        inventoryId: '',
        startTime: '',
        endTime: ''
      }
    }
  },
  created() {
    this.initQueryParams()
    this.getList()
  },
  methods: {
    /** 初始化查询参数 */
    initQueryParams() {
      this.queryForm.huoweiId = this.huoweiId
      this.queryForm.drugId = this.drugId
      this.queryForm.drugBatchNo = this.drugBatchNo
      if (this.queryParams.inventoryId) {
        this.queryForm.inventoryId = this.queryParams.inventoryId
      }
      if (this.queryParams.startTime && this.queryParams.endTime) {
        this.dateRange = [this.queryParams.startTime, this.queryParams.endTime]
        this.queryForm.startTime = this.queryParams.startTime
        this.queryForm.endTime = this.queryParams.endTime
      }
    },

    /** 查询变更详情列表 */
    getList() {
      this.loading = true
      if (this.dateRange && this.dateRange.length === 2) {
        this.queryForm.startTime = this.dateRange[0]
        this.queryForm.endTime = this.dateRange[1]
      } else {
        this.queryForm.startTime = ''
        this.queryForm.endTime = ''
      }

      getChangeDetails(this.queryForm).then(response => {
        this.changeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryForm.pageNum = 1
      this.getList()
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.$refs.queryForm.resetFields()
      // 保持基本查询条件
      this.queryForm.huoweiId = this.huoweiId
      this.queryForm.drugId = this.drugId
      this.queryForm.drugBatchNo = this.drugBatchNo
      this.handleQuery()
    },

    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },

    /** 导出按钮操作 */
    handleExport() {
      this.$download.excel(this.queryForm, `变更详情_${new Date().getTime()}.xlsx`)
    },

    /** 获取变更类型标签类型 */
    getChangeTypeTagType(type) {
      const typeMap = {
        '正常': 'success',
        '数量更改': 'warning',
        '货位调换': 'info',
        '效期更改': 'danger',
        '批号更改': 'primary',
        '其它信息': ''
      }
      return typeMap[type] || ''
    },

    /** 格式化时间 */
    parseTime(time, pattern) {
      return parseTime(time, pattern)
    }
  }
}
</script>

<style scoped>
.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding: 10px 0;
  border-bottom: 1px solid #e4e7ed;
}

.page-header h3 {
  margin: 0;
  color: #303133;
}

.drug-info-card {
  margin-bottom: 20px;
}

.info-item {
  margin-bottom: 10px;
}

.info-item label {
  font-weight: bold;
  color: #606266;
}

.info-item span {
  color: #303133;
}
</style>
